package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
)

var parent string
var ignore string

// git -c core.quotepath=false -c log.showSignature=false checkout -b release/1.0.5 origin/release/1.0.5
func main() {
	parent, _ := os.Getwd()
	fmt.Println("exec in path:", parent)
	fmt.Println(os.Args[1:])
	gitArgs := os.Args[1:]
	dirs, err := ioutil.ReadDir(parent)
	handleErr(err)
	for _, dir := range dirs {
		if dir.IsDir() {
			if !strings.Contains(ignore, dir.Name()) {
				fmt.Printf("%s\t", dir.Name())
				cmd := exec.Command("git", gitArgs...)
				cmd.Dir = parent + string(os.PathSeparator) + dir.Name()
				cmd.Stdout = os.Stdout
				cmd.Stderr = os.Stderr
				err = cmd.Run()
				if err != nil {
					fmt.Println("cmd Error: ", err)
					continue
				}
			}
		}
	}

}

func handleErr(err error) {
	if err != nil {
		panic(err)
	}
}

func init() {
	ignore = ".git|ircloud-parent|ydh-parent|ircloud-config|ircloud-sql-config"
}
